using UnityEngine;

public class TitanController : MonoBehaviour
{
	public float moveSpeed = 1f;

	public float rotationSpeed = 5f;

	public float armRotateSpeed = 2.5f;

	public float jumpSpeed = 10f;

	public float directionLerp = 5f;

	public float gravity = 20f;

	public TitanBehavior titanBehavior;

	public CharacterController characterController;

	public bool isGrounded;

	public Vector3 currentDirection = Vector3.zero;

	public Vector3 lerpedDirection = Vector3.zero;

	public bool usingJetpack;

	public bool usedAllJetpackFule;

	public float topPartRotationSpeed = 15f;

	public float bottomPartRotationSpeed = 5f;

	public float angelEpsilon = 15f;

	public Quaternion titanBottomPartRotation;

	public float currentRotation;

	private void Update()
	{
		if (!PlayerBehavior.IsMyCurrentVehicle(titanBehavior))
		{
			if (titanBehavior.isDead)
			{
				characterController.Move(new Vector3(0f, -0.25f, 0f));
			}
			else
			{
				characterController.Move(new Vector3(0f, -0.05f, 0f));
			}
			isGrounded = characterController.isGrounded;
			return;
		}
		isGrounded = characterController.isGrounded;
		float x = Input.GetAxis("Horizontal");
		float z = Input.GetAxis("Vertical");
		if (JoystickCarControll.joysticPosition.y != 0f)
		{
			x = JoystickCarControll.joysticPosition.x;
		}
		if (JoystickCarControll.joysticPosition.x != 0f)
		{
			z = JoystickCarControll.joysticPosition.y;
		}
		currentDirection = new Vector3(x, 0f, z);
		currentDirection = TPSCamera.instance.transform.TransformDirection(currentDirection);
		UseJetpack();
		currentDirection = new Vector3(currentDirection.x * moveSpeed, currentDirection.y * titanBehavior.jumpSpeed, currentDirection.z * moveSpeed);
		currentDirection.y -= gravity;
		float num = 100f;
		if (currentDirection.y < lerpedDirection.y)
		{
			num = directionLerp / 3f;
		}
		titanBehavior.topTitanPart.rotation = Quaternion.Lerp(titanBehavior.topTitanPart.rotation, Quaternion.Euler(0f, TPSCamera.instance.transform.rotation.eulerAngles.y, 0f), Time.deltaTime * topPartRotationSpeed);
		if (isGrounded && (currentDirection.x != 0f || currentDirection.z != 0f))
		{
			titanBottomPartRotation = Quaternion.Euler(0f, Quaternion.LookRotation(currentDirection).eulerAngles.y, 0f);
			currentRotation = titanBehavior.bottomTitanPart.rotation.eulerAngles.y - titanBottomPartRotation.eulerAngles.y;
			titanBehavior.bottomTitanPart.rotation = Quaternion.Lerp(titanBehavior.bottomTitanPart.rotation, titanBottomPartRotation, Time.deltaTime * bottomPartRotationSpeed);
			if (Mathf.Abs(titanBehavior.bottomTitanPart.rotation.eulerAngles.y - titanBottomPartRotation.eulerAngles.y) > angelEpsilon)
			{
				currentDirection.x = 0f;
				currentDirection.z = 0f;
			}
		}
		else
		{
			currentRotation = 0f;
		}
		if (titanBehavior.leftArmTransform != null && titanBehavior.rightArmTransform != null)
		{
			Quaternion to = titanBehavior.TargetDetector.GetQuaternionXToTarget() * titanBehavior.initialArmsRotation;
			titanBehavior.leftArmTransform.localRotation = Quaternion.RotateTowards(titanBehavior.leftArmTransform.localRotation, to, armRotateSpeed);
			titanBehavior.rightArmTransform.localRotation = titanBehavior.leftArmTransform.localRotation;
		}
		lerpedDirection.x = Mathf.Lerp(lerpedDirection.x, currentDirection.x, Time.deltaTime * directionLerp);
		lerpedDirection.z = Mathf.Lerp(lerpedDirection.z, currentDirection.z, Time.deltaTime * directionLerp);
		lerpedDirection.y = Mathf.Lerp(lerpedDirection.y, currentDirection.y, Time.deltaTime * num);
		characterController.Move(lerpedDirection * Time.deltaTime);
	}

	private void UseJetpack()
	{
		if (usingJetpack && titanBehavior.jetpackFuel > 0f)
		{
			if (titanBehavior.jetpackFuel > 0f)
			{
				titanBehavior.jetpackFuel -= titanBehavior.spendJetpackFuelWithTime * Time.deltaTime;
			}
			if (titanBehavior.jetpackFuel > 1f)
			{
				usedAllJetpackFule = false;
			}
			else
			{
				usedAllJetpackFule = true;
			}
			if (!usedAllJetpackFule)
			{
				currentDirection.y = jumpSpeed;
			}
		}
	}
}
